package com.lanou.renrenxin.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lanou.renrenxin.R;
import com.lanou.renrenxin.activity.ChattingActivity;
import com.lanou.renrenxin.adapter.ChatListAdapter;
import com.lanou.renrenxin.onclicklistener.OnChatListItemClickListener;
import com.lanou.renrenxin.tools.DividerItemDecoration;

/**
 * Created by zy on 16/3/1.
 */
public class ChatFragment extends Fragment implements OnChatListItemClickListener {
    private RecyclerView chatList;
    private ChatListAdapter adapter;
    private Context context;
    private String mUserName;
    private String toUserName;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        context = activity;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_chat,container,false);
        chatList = (RecyclerView) view.findViewById(R.id.recyclerView_fragment_chat);
        return view;

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Bundle bundle = getArguments();
        mUserName = bundle.getString("USER_NAME_MAIN");
        toUserName = bundle.getString("TO_USERNAME");
        adapter = new ChatListAdapter(context,toUserName,mUserName);
        adapter.setOnChatListItemClickListener(this);
        chatList.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL_LIST));
        chatList.setLayoutManager(new LinearLayoutManager(context));
        chatList.setAdapter(adapter);
    }

    @Override
    public void onResume() {
        super.onResume();
        adapter.refreshData();

    }
    @Override
    public void onItemClick(String username) {
        Intent intent = new Intent(context, ChattingActivity.class);
        intent.putExtra("USER_NAME",username);
        intent.putExtra("M_USER_NAME",mUserName);
        startActivity(intent);
    }
}
